<?php

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\Template as TemplateModel;
use Illuminate\Support\Facades\DB;

class TemplateServices extends CommonServices
{
    // TODO 复用模板特性
    use TemplateTraitServices;

    protected $templateModel;

    public function __construct()
    {
        $this->modelInstance();

        $this->templateModel = new TemplateModel();
    }

    /**
     * 获取页面数据源
     * */
    public function getTemplateView()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('view_alias_name', '页面别名', 'string', true, '页面别名不能为空');
            $this->I('user_id', '用户ID', 'int', false);

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            // 获取视图
            $getTemplateView = $this->templateModel->getTemplateViewCache($this->requestData['view_alias_name'], $this->requestData['user_id'] ?? 0);
            if (empty($getTemplateView)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '未配置主页元素';
                break;
            }
            if ($getTemplateView['layout_id'] > 0 and empty($getTemplateView['template_block'])) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '未配置主页块元素';
                break;
            }

            $this->parseBlockFormatData($getTemplateView['template_block']);

            $data = $getTemplateView;

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 获取块级数据源
     * */
    public function getTemplateBlockData()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = (object)[];

        do {
            $this->I('view_alias_name', '页面别名', 'string', false);
            $this->I('user_id', '用户ID', 'int', false);
            $this->I('block_id', '块级ID', 'int', true, '块级ID不能为空');
            $this->I('page', '页数', 'int', false, '页数不能为空');
            $this->I('limit', '条数', 'int', false, '条数不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            // 获取块级数据
            $getTemplateBlock = $this->templateModel->getTemplateViewCache($this->requestData['view_alias_name'] ?? 'home', $this->requestData['user_id'] ?? 0, $this->requestData['block_id']);
            if (empty($getTemplateBlock)) {
                // $code = Hint::ERR_DATA_NOT_EXIST;
                // $msg = '块级' . Hint::getErrCodeMsg($code);
                break;
            }

            $getTemplateBlock = [$getTemplateBlock];
            $this->parseBlockFormatData($getTemplateBlock, $this->requestData['page'] ?? 0, $this->requestData['limit'] ?? 0);

            $data = $getTemplateBlock[0] ?? [];

        } while (false);

        return compact('code', 'msg', 'data');
    }

    ################# 后台调用 ####################################################################################

    /**
     * 页面缓存-设置预览用户
     * */
    public function setTemplateViewPreviewUser()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('type', '操作类型', 'int', false);// 默认 0=读取，1=写入
            $this->I('user_ids', '用户ID', 'string', false, '用户ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $type = $this->requestData['type'] ?? 0;
            if (!in_array($type, [0, 1])) {
                $code = Hint::ERR_PARAMS_CHECK_FAIL;
                $msg = '操作类型不匹配';
                break;
            }

            $userIds = '';
            if ($type == 1) {
                if (!isset($this->requestData['user_ids']) or empty($this->requestData['user_ids'])) {
                    $code = Hint::ERR_PARAMS_CHECK_FAIL;
                    $msg = '预览用户ID值不能为空.';
                    break;
                }

                $userIdArray = explode(',', $this->requestData['user_ids']);
                $userIdArray = array_filter($userIdArray);
                if (empty($userIdArray)) {
                    $code = Hint::ERR_PARAMS_CHECK_FAIL;
                    $msg = '预览用户ID值不能为空!';
                    break;
                }
                $userIds = join(',', $userIdArray);
            }

            $data['user_ids'] = $this->templateModel->setTemplateViewPreviewUserCache($type, $userIds);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 页面缓存-发布
     * */
    public function setTemplateViewRelease()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $getTemplateViewList = $this->templateModel->getTemplateViewList();
            if (empty($getTemplateViewList)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '查询所有配置页面' . Hint::getErrCodeMsg($code);
                break;
            }

            foreach ($getTemplateViewList as $key => $value) {
                $ret = $this->templateModel->setTemplateViewReleaseCache($value['view_alias_name']);
                if (!$ret) {
                    $code = Hint::ERR_FAIL;
                    $msg = $value['view_name'] . '发布' . Hint::getErrCodeMsg($code);
                    break;
                }
            }

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 创建视图
     * */
    public function createTemplateView()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('view_name', '页面名称', 'string', true, '页面名称不能为空');
            $this->I('view_alias_name', '页面别名', 'string', true, '页面别名不能为空');// 唯一值（例如：home）
            $this->I('action', '页面跳转指令', 'string', true, '页面跳转指令不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateViewSingle = $this->templateModel->getTemplateViewSingle(['view_alias_name' => $this->requestData['view_alias_name']]);
            if (!empty($getTemplateViewSingle)) {
                $code = Hint::ERR_DATA_EXIST;
                $msg = '页面别名已存在，请检查后重试';
                break;
            }

            $this->requestData['type'] = 1;// 表示动态页面
            $ret = $this->templateModel->createTemplateView($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_CREATE_FAIL;
                $msg = '页面' . Hint::getErrCodeMsg($code);
                break;
            }

            $data['view_id'] = intval($ret);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 更新视图
     * */
    public function updateTemplateView()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('view_id', '页面ID', 'int', true, '页面ID不能为空');
            $this->I('layout_id', '布局ID', 'int', true, '布局ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateViewSingle = $this->templateModel->getTemplateViewSingle(['view_id' => $this->requestData['view_id']]);
            if (empty($getTemplateViewSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '页面' . Hint::getErrCodeMsg($code);
                break;
            }

            $getTemplateLayoutSingle = $this->templateModel->getTemplateLayoutSingle(['layout_id' => $this->requestData['layout_id']]);
            if (empty($getTemplateLayoutSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '布局' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->updateTemplateView(['view_id' => $this->requestData['view_id']], ['layout_id' => $this->requestData['layout_id']]);
            if (!$ret) {
                $code = Hint::ERR_DATA_UPDATE_FAIL;
                $msg = '页面' . Hint::getErrCodeMsg($code);
                break;
            }

            $data = $this->requestData;

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 删除视图
     * */
    public function removeTemplateView()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        // 事物开启
        DB::beginTransaction();

        do {
            $this->I('view_id', '页面ID', 'int', true, '页面ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateViewSingle = $this->templateModel->getTemplateViewSingle($this->requestData);
            if (empty($getTemplateViewSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '页面' . Hint::getErrCodeMsg($code);
                break;
            }

            // 删除视图数据
            $ret = $this->templateModel->removeTemplateView($this->requestData['view_id']);
            if (!$ret) {
                $code = Hint::ERR_DATA_REMOVE_FAIL;
                $msg = '页面' . Hint::getErrCodeMsg($code);
                break;
            }

            // 删除布局数据
            $getTemplateLayoutSingle = $this->templateModel->getTemplateLayoutSingle($this->requestData);
            if (!empty($getTemplateLayoutSingle)) {
                $ret = $this->templateModel->removeTemplateLayout($this->requestData);
                if (!$ret) {
                    $code = Hint::ERR_DATA_REMOVE_FAIL;
                    $msg = '布局' . Hint::getErrCodeMsg($code);
                    break;
                }

                // 删除块级数据
                $getTemplateBlockSingle = $this->templateModel->getTemplateBlockSingle(['layout_id' => $getTemplateLayoutSingle['layout_id']]);
                if (!empty($getTemplateBlockSingle)) {
                    $ret = $this->templateModel->removeTemplateBlock(['layout_id' => $getTemplateLayoutSingle['layout_id']]);
                    if (!$ret) {
                        $code = Hint::ERR_DATA_REMOVE_FAIL;
                        $msg = '块级' . Hint::getErrCodeMsg($code);
                        break;
                    }
                }
            }

        } while (false);

        if ($code == Hint::SUCCESS) {
            // 事物回滚
            DB::commit();
        } else {
            // 事物回滚
            DB::rollBack();
        }

        return compact('code', 'msg', 'data');
    }

    /**
     * 创建布局
     * */
    public function createTemplateLayout()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('view_id', '页面ID', 'int', true, '页面ID不能为空');
            $this->I('layout_name', '布局名称', 'string', true, '布局名称不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateViewSingle = $this->templateModel->getTemplateViewSingle(['view_id' => $this->requestData['view_id']]);
            if (empty($getTemplateViewSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '页面' . Hint::getErrCodeMsg($code);
                break;
            }

            $getTemplateLayoutSingle = $this->templateModel->getTemplateLayoutSingle($this->requestData);
            if (!empty($getTemplateLayoutSingle)) {
                $code = Hint::ERR_DATA_EXIST;
                $msg = '布局' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->createTemplateLayout($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_CREATE_FAIL;
                $msg = '布局' . Hint::getErrCodeMsg($code);
                break;
            }

            $data['layout_id'] = intval($ret);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 删除布局
     * */
    public function removeTemplateLayout()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        // 事物开启
        DB::beginTransaction();

        do {
            $this->I('layout_id', '布局ID', 'int', true, '布局ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateLayoutSingle = $this->templateModel->getTemplateLayoutSingle($this->requestData);
            if (empty($getTemplateLayoutSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '布局' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->removeTemplateLayout($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_REMOVE_FAIL;
                $msg = '布局' . Hint::getErrCodeMsg($code);
                break;
            }

            // 删除块级数据
            $getTemplateBlockSingle = $this->templateModel->getTemplateBlockSingle($this->requestData);
            if (!empty($getTemplateBlockSingle)) {
                $ret = $this->templateModel->removeTemplateBlock($this->requestData);
                if (!$ret) {
                    $code = Hint::ERR_DATA_REMOVE_FAIL;
                    $msg = '块级' . Hint::getErrCodeMsg($code);
                    break;
                }
            }

            // 更新视图保留的布局ID为0
            $getTemplateViewSingle = $this->templateModel->getTemplateViewSingle($this->requestData);
            if (!empty($getTemplateViewSingle)) {
                $ret = $this->templateModel->updateTemplateView($this->requestData, ['layout_id' => 0]);
                if (!$ret) {
                    $code = Hint::ERR_DATA_UPDATE_FAIL;
                    $msg = '更新页面保留的布局ID为0' . Hint::getErrCodeMsg($code);
                    break;
                }
            }

        } while (false);

        if ($code == Hint::SUCCESS) {
            // 事物回滚
            DB::commit();
        } else {
            // 事物回滚
            DB::rollBack();
        }

        return compact('code', 'msg', 'data');
    }

    /**
     * 更新/创建块级
     * */
    public function updateOrInsertTemplateBlock()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('block_id', '块级ID', 'int', false, '块级ID不能为空');// 更新时需要传入此参数
            $this->I('block_name', '模块名称', 'string', false);
            $this->I('layout_id', '布局ID', 'int', true, '布局ID不能为空');
            $this->I('type_id', '类型ID', 'int', true, '类型ID不能为空');// 0=自定义，1=房间，2=用户，3=留声机
            $this->I('style_id', '样式ID', 'int', true, '样式ID不能为空');
            $this->I('source_id', '数据来源ID', 'int', true, '数据来源ID不能为空');// 0=手动添加
            $this->I('element_number', '元素个数', 'int', true, '元素个数不能为空');// 0=不限，1=1个 .....
            $this->I('data_source', '数据源', 'string', false);// 类型ID为 0=自定义 或 数据源ID为 0=手动添加 时必填
            $this->I('action', '更多icon设置跳转指令', 'string', false);
            $this->I('is_show_block', '是否显示模板块', 'int', true, '是否显示模板块不能为空');// 0=不显示，1=显示
            $this->I('sort_number', '排列序号', 'int', true, '排列序号不能为空');// 默认99往后排
            $this->I('is_page', '是否分页', 'int', true, '是否分页不能为空');// 0=否，1=是

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            // 块级ID
            $blockId = $this->requestData['block_id'] ?? 0;
            unset($this->requestData['block_id']);

            if (isset($this->requestData['data_source']) and is_string($this->requestData['data_source'])) {
                $this->requestData['data_source'] = json_decode($this->requestData['data_source'], true);
                if (!is_array($this->requestData['data_source'])) {
                    $code = Hint::ERR_PARAMS_CHECK_FAIL;
                    $msg = '数据源参数格式错误';
                    break;
                }
            }

            // 类型ID为 0=自定义 时判断数据源不能为空
            // 或
            // 数据来源ID为 0=手动添加 时判断数据源不能为空
            if (($this->requestData['type_id'] == 0 or $this->requestData['source_id'] == 0) and empty($this->requestData['data_source'])) {
                $code = Hint::ERR_PARAMS_CHECK_FAIL;
                $msg = '手动添加数据源不能为空';
                break;
            }
            // 类型ID 0=自定义 时校验数据源参数
            // 或
            // 类型ID >0 && 来源ID 0=手动 时校验数据源参数
            if ($this->requestData['type_id'] == 0 or ($this->requestData['type_id'] > 0 and $this->requestData['source_id'] == 0)) {
                $checkCustomDataSource = $this->checkTypeIdDataSourceParams($this->requestData['type_id'], $this->requestData['data_source']);
                if (!$checkCustomDataSource) {
                    $code = Hint::ERR_PARAMS_CHECK_FAIL;
                    $msg = '自定义类型数据源：' . Hint::getErrCodeMsg($code);
                    break;
                }
            }
            if (isset($this->requestData['data_source']) and is_array($this->requestData['data_source'])) {
                $this->requestData['data_source'] = json_encode($this->requestData['data_source'], JSON_UNESCAPED_UNICODE);
            }

            $getTemplateLayoutSingle = $this->templateModel->getTemplateLayoutSingle(['layout_id' => $this->requestData['layout_id']]);
            if (empty($getTemplateLayoutSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '布局' . Hint::getErrCodeMsg($code);
                break;
            }

            // 查询类型
            if ($this->requestData['type_id'] > 0) {
                $getTemplateBlockTypeSingle = $this->templateModel->getTemplateBlockTypeSingle(['type_id' => $this->requestData['type_id']]);
                if (empty($getTemplateBlockTypeSingle)) {
                    $code = Hint::ERR_DATA_NOT_EXIST;
                    $msg = '类型' . Hint::getErrCodeMsg($code);
                    break;
                }
            }

            // 查询样式
            $getTemplateBlockStyleSingle = $this->templateModel->getTemplateBlockStyleSingle(['style_id' => $this->requestData['style_id']]);
            if (empty($getTemplateBlockStyleSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '样式' . Hint::getErrCodeMsg($code);
                break;
            }

            // 查询数据来源
            if ($this->requestData['source_id'] > 0) {
                $getTemplateBlockSourceSingle = $this->templateModel->getTemplateBlockSourceSingle(['source_id' => $this->requestData['source_id']]);
                if (empty($getTemplateBlockSourceSingle)) {
                    $code = Hint::ERR_DATA_NOT_EXIST;
                    $msg = '数据来源' . Hint::getErrCodeMsg($code);
                    break;
                }
            }

            // 更新
            if ($blockId > 0) {
                $getTemplateBlockSingle = $this->templateModel->getTemplateBlockSingle(['block_id' => $blockId, 'layout_id' => $this->requestData['layout_id']]);
                if (empty($getTemplateBlockSingle)) {
                    $code = Hint::ERR_DATA_NOT_EXIST;
                    $msg = '块级' . Hint::getErrCodeMsg($code);
                    break;
                }

                $ret = $this->templateModel->updateTemplateBlock(['block_id' => $blockId], $this->requestData);
                if (!$ret) {
                    $code = Hint::ERR_DATA_UPDATE_FAIL;
                    $msg = '块级' . Hint::getErrCodeMsg($code);
                    break;
                }
                $data['block_id'] = $blockId;
                break;
            }

            // 创建
            $ret = $this->templateModel->createTemplateBlock($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_CREATE_FAIL;
                $msg = '块级' . Hint::getErrCodeMsg($code);
                break;
            }
            $data['block_id'] = intval($ret);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 块级排序
     * */
    public function sortTemplateBlock()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('block_id', '块级ID', 'int', true, '块级ID不能为空');
            $this->I('sort_number', '排列序号', 'int', true, '排列序号不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateBlockSingle = $this->templateModel->getTemplateBlockSingle(['block_id' => $this->requestData['block_id']]);
            if (empty($getTemplateBlockSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '块级' . Hint::getErrCodeMsg($code);
                break;
            }

            // 更新
            $ret = $this->templateModel->updateTemplateBlock(['block_id' => $this->requestData['block_id']], ['sort_number' => $this->requestData['sort_number']]);
            if (!$ret) {
                $code = Hint::ERR_DATA_UPDATE_FAIL;
                $msg = '块级' . Hint::getErrCodeMsg($code);
                break;
            }

            $data['block_id'] = $this->requestData['block_id'];

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 删除块级
     * */
    public function removeTemplateBlock()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('block_id', '块级ID', 'int', true, '块级ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateBlockSingle = $this->templateModel->getTemplateBlockSingle($this->requestData);
            if (empty($getTemplateBlockSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '块级' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->removeTemplateBlock($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_REMOVE_FAIL;
                $msg = '块级' . Hint::getErrCodeMsg($code);
                break;
            }
        } while (false);

        return compact('code', 'msg', 'data');
    }

    ################# 以下逻辑暂时忽略 不对外开放 ####################################################################################

    /**
     * 创建块级-类型
     * */
    public function createTemplateBlockType()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('type_name', '类型名称', 'string', true, '类型名称不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateBlockTypeSingle = $this->templateModel->getTemplateBlockTypeSingle($this->requestData);
            if (!empty($getTemplateBlockTypeSingle)) {
                $code = Hint::ERR_DATA_EXIST;
                $msg = '类型' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->createTemplateBlockType($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_CREATE_FAIL;
                $msg = '类型' . Hint::getErrCodeMsg($code);
                break;
            }

            $data['type_id'] = intval($ret);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 删除块级-类型
     * */
    public function removeTemplateBlockType()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('type_id', '类型ID', 'int', true, '类型ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateBlockTypeSingle = $this->templateModel->getTemplateBlockTypeSingle($this->requestData);
            if (empty($getTemplateBlockTypeSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '类型' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->removeTemplateBlockType($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_REMOVE_FAIL;
                $msg = '类型' . Hint::getErrCodeMsg($code);
                break;
            }

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 创建块级-样式
     * */
    public function createTemplateBlockStyle()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('type_id', '类型ID', 'int', true, '类型ID不能为空');
            $this->I('style_name', '样式名称', 'string', true, '样式名称不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateBlockTypeSingle = $this->templateModel->getTemplateBlockTypeSingle(['type_id' => $this->requestData['type_id']]);
            if (empty($getTemplateBlockTypeSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '类型' . Hint::getErrCodeMsg($code);
                break;
            }

            $getTemplateBlockStyleSingle = $this->templateModel->getTemplateBlockStyleSingle($this->requestData);
            if (!empty($getTemplateBlockStyleSingle)) {
                $code = Hint::ERR_DATA_EXIST;
                $msg = '样式' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->createTemplateBlockStyle($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_CREATE_FAIL;
                $msg = '样式' . Hint::getErrCodeMsg($code);
                break;
            }

            $data['style_id'] = intval($ret);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 删除块级-样式
     * */
    public function removeTemplateBlockStyle()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('style_id', '样式ID', 'int', true, '样式ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateBlockStyleSingle = $this->templateModel->getTemplateBlockStyleSingle($this->requestData);
            if (empty($getTemplateBlockStyleSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '样式' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->removeTemplateBlockStyle($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_REMOVE_FAIL;
                $msg = '样式' . Hint::getErrCodeMsg($code);
                break;
            }

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 创建块级数据来源
     * */
    public function createTemplateBlockSource()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('type_id', '类型ID', 'int', true, '类型ID不能为空');
            $this->I('source_name', '数据来源名称', 'string', true, '数据来源名称不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateBlockTypeSingle = $this->templateModel->getTemplateBlockTypeSingle(['type_id' => $this->requestData['type_id']]);
            if (empty($getTemplateBlockTypeSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '类型' . Hint::getErrCodeMsg($code);
                break;
            }

            $getTemplateBlockSourceSingle = $this->templateModel->getTemplateBlockSourceSingle($this->requestData);
            if (!empty($getTemplateBlockSourceSingle)) {
                $code = Hint::ERR_DATA_EXIST;
                $msg = '数据来源' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->createTemplateBlockSource($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_CREATE_FAIL;
                $msg = '数据来源' . Hint::getErrCodeMsg($code);
                break;
            }

            $data['source_id'] = intval($ret);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 删除块级数据来源
     * */
    public function removeTemplateBlockSource()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('source_id', '数据来源ID', 'int', true, '数据来源ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $getTemplateBlockSourceSingle = $this->templateModel->getTemplateBlockSourceSingle($this->requestData);
            if (empty($getTemplateBlockSourceSingle)) {
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '数据来源' . Hint::getErrCodeMsg($code);
                break;
            }

            $ret = $this->templateModel->removeTemplateBlockSource($this->requestData);
            if (!$ret) {
                $code = Hint::ERR_DATA_REMOVE_FAIL;
                $msg = '数据来源' . Hint::getErrCodeMsg($code);
                break;
            }

        } while (false);

        return compact('code', 'msg', 'data');
    }
}